package com.duowan.realtime.scheduled.batch.processor;

import com.duowan.realtime.model.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @version 1.0
 * @author: tangsicheng
 * @date: 14-9-5
 * @since 1.0
 */
public class RedisKVDefaultProcessor implements ItemProcessor<Map, List<Map>> {

    private static final Logger logger = LoggerFactory.getLogger(RedisKVDefaultProcessor.class);

    @Override
    public List<Map> process(Map item) throws Exception {
        List<Map> resultList = new LinkedList<Map>();
        Set<Entry> entries = item.entrySet();
        for (Entry entry : entries) {
            try {
                resultList.add(getKVMap(entry));
            } catch (Exception e) {
                logger.error("获取redis kv 错误", e);
                continue;
            }
        }
        return resultList;
    }

    private Map getKVMap(Entry entry) {
        String key = (String) entry.getKey();
        Object value = entry.getValue();
        Map<String, Object> kvMap = RedisKey.factory(key).toMap();
        kvMap.put("kpiValue", value);
        return kvMap;
    }
}
